using System.Collections.Generic;
using System;
using System.Linq;

namespace Prota
{
    public struct ValuePullListener<T>
    {
        public T value;
        public bool Pull(T newValue)
        {
            if(!EqualityComparer<T>.Default.Equals(value, newValue))
            {
                value = newValue;
                return true;
            }
            return false;
        }
    }
    
    // 当我们想要监听一个值的变化时，我们可以使用这个.
    public struct ValueListener<T>
    {
        public T value;
        public readonly bool inited;
        public Action<T, T> onValueChanged;
        public Func<T> getter;
        
        public void Poll()
        {
            if (inited && getter != null)
            {
                T newValue = getter();
                if (!EqualityComparer<T>.Default.Equals(value, newValue))
                {
                    var oldValue = value;
                    value = newValue;
                    onValueChanged?.Invoke(oldValue, value);
                }
            }
        }
    }
}
